PROPERTY:  RegExp::lastIndexNetscape Only Feature


This property is the index at which to start the next match, but is only set if the regular expression uses the 'g' flag to specify a global search. It consists of an integer specifying the index (counting from the beginning of the string and including all alphanumeric and non-alphanumeric characters) of the first character after the last match. So, for example, the following code searches for an occurrence of the substring 'ships' in the string "the hardships of traveling", matches the word 'hardships', and then displays the number 14, which is the index of the letter 'o' of 'of'. Note that the regular expression includes the white (blank) space character \s.
 
Code:
myRexp = /ships*\s/g
myRexp.exec("the hardships of traveling")
document.write(myRexp.lastIndex)

 
Output:
14
 
If the value of the lastIndex property is greater than the length of the string, then both the test and exec methods will fail, and the lastIndex property will be set to 0. For example, the following code will match the final 'ing' of 'traveling' and set the lastIndex property to 26 (one more than the index of the last character of the string). If you then immediately run the match again, it will fail and the lastIndex property will be set to 0.
 
Code:
myRexp = /ing/g
myRexp.exec("the hardships of traveling")

 
If the lastIndex property is the same as the length of the string, and provided the regular expression doesn't match an empty string (by using '?'), then there will be no match and the lastIndex property will be set to 0. If, however, the regular expression does match an empty string, then the regular expression will match the empty string at lastIndex.


Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information